import { BaseWindow } from "../../../../component/window/BaseWindow";
import { IGuildPartyModule } from "../../../../../modules/interfaces/IGuildPartyModule";
import { ILayer } from "../../../../mvc/interfaces/ILayer";
import { Language } from "../../../../../extend/language/Language";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { GuildPartyConfig } from "../../../resource/GuildPartyConfig";
import { ObjCreate } from "../../common/ObjCreate";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { RadioButtonGroup } from "../../../../../fl/controls/RadioButtonGroup";
import { TGuildPartyConfig } from "../../../../../Message/Db/Tables/TGuildPartyConfig";
import { EGuildPartyType } from "../../../../../Message/Game/EGuildPartyType";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GuildPartyInfo } from "../info/GuildPartyInfo";
import { ClockManager } from "../../../manager/ClockManager";
import { GuildPartyDiscountInfo } from "../info/GuildPartyDiscountInfo";
import { GRadioButton } from "../../../../../com/mui/controls/GRadioButton";
import { EffectManager } from "../../../manager/EffectManager";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { MsgManager } from "../../../manager/MsgManager";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { Cache } from "../../../cache/Cache";
import { GuildUtil } from "../info/GuildUtil";
import { GTextFiled } from "../../../../../com/mui/controls/GTextFiled";
type int = number;
//class GuildPartyWindow
    
    export  class GuildPartyWindow extends BaseWindow implements IGuildPartyModule
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this._timeBtnArray = [];
            this._partyConfigArray = [];
            this._radioArray = [];
            this._selectSpriteArray = [];
            this._typeTxtArray = [];
            this.setSize(325, 415);
            this.titleHeight = 28;
            this.title = Language.getString(57414);
            return;
        }

        /* internal  */onRewardTxtClickHandler(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildPartyRewardWindowOpen));
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=null;
            super.createChildren();
            this._partyConfigArray = GuildPartyConfig.instance.partyConfigArray;
            ObjCreate.createTextField(Language.getString(57415), 22, 45, 220, 20, this, GlobalStyle.textFormat6);
            ObjCreate.createBg(20, 65, 290, 95, this);
            this._typeRadioGroup = new RadioButtonGroup("type");
            loc5 = 0;
            while (loc5 < this._partyConfigArray.length) 
            {
                loc1 = this._partyConfigArray[loc5] as TGuildPartyConfig;
                loc2 = ObjCreate.createRadioButton("", 25, 68 + loc5 * 30, 100, 28, this);
                loc3 = ObjCreate.createTextField("", 45, 72 + loc5 * 30, 220, 20, this);
                loc3.htmlText = Language.getStringByParam(57422 + loc5, loc1.costGold);
                loc3.mouseEnabled = false;
                this._typeTxtArray.push(loc3);
                loc2.group = this._typeRadioGroup;
                (loc4 = new Object /* flash.display.Sprite */()).name = loc5.toString();
                loc4.graphics.beginFill(0, 0);
                loc4.graphics.drawRect(45, 72 + loc5 * 30, 215, 20);
                loc4.graphics.endFill();
                loc4.cacheAsBitmap = true;
                loc4.addEventListener(flash.events.MouseEvent.CLICK, this.onSelectClickHandler);
                this.addChild(loc4);
                this._selectSpriteArray.push(loc4);
                if (loc5 != 0) 
                {
                    if (loc5 != 1) 
                    {
                        if (loc5 == 2) 
                        {
                            loc2.value = EGuildPartyType._EGuildPartySenior;
                            loc2.selected = true;
                        }
                    }
                    else 
                    {
                        loc2.value = EGuildPartyType._EGuildPartyMedium;
                    }
                }
                else 
                {
                    loc2.value = EGuildPartyType._EGuildPartyPrimary;
                }
                this._radioArray.push(loc2);
                ++loc5;
            }
            this._partyRewardTxt = ObjCreate.createGTextField("", 210, 105, 220, 20, this, new GTextFormat(FontUtil.songtiName, 12, 16711680));
            this._partyRewardTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._partyRewardTxt.htmlText = Language.getString(57623);
            this._partyRewardTxt.addEventListener(flash.events.TextEvent.LINK, this.onRewardTxtClickHandler);
            ObjCreate.createTextField(Language.getString(57416), 22, 165, 220, 20, this, GlobalStyle.textFormat6);
            this._timeTxt = ObjCreate.createTextField("", 0, 185, 325, 20, this, new GTextFormat(FontUtil.songtiName, 12, 65280));
            this._timeTxt.htmlText = Language.getStringByParam(57417, "？");
            this._timeTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            ObjCreate.createBg(20, 200, 290, 102, this, ImagesConst.PreperenBg);
            var loc7=9;
            while (loc7 <= 22) 
            {
                (loc6 = ObjCreate.createGButton(loc7.toString(), 48 + (loc7 - 9) % 7 * 35, 220 + Math.floor((loc7 - 9) / 7) * 35, 30, 28, this, "GuildPartyTimeBtn")).name = loc7.toString();
                loc6.addEventListener(flash.events.MouseEvent.CLICK, this.onTimeBtnClickHandler);
                this._timeBtnArray.push(loc6);
                ++loc7;
            }
            this._descBtn = ObjCreate.createGButton(Language.getString(57418), 85, 315, 70, 28, this);
            this._descBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onDescBtnClickHandler);
            this._createBtn = ObjCreate.createGButton(Language.getString(57419), 170, 315, 70, 28, this);
            this._createBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onCreateBtnClickHandler);
            ObjCreate.createTextField(Language.getString(57470), 50, 355, 220, 20, null, GlobalStyle.textFormat6);
            this._raiseTxt = ObjCreate.createTextField("0", 115, 355, 220, 20, null, GlobalStyle.textFormat6);
            ObjCreate.createTextField(Language.getString(57421), 50, 365, 220, 20, this, GlobalStyle.textFormat6);
            this._goldTxt = ObjCreate.createTextField("", 115, 365, 220, 20, this, GlobalStyle.textFormat6);
            return;
        }

        public updateByGuildPartyInfo(arg1: GuildPartyInfo): void
        {
            this._partyInfo = arg1;
            if (arg1) 
            {
                this._raiseTxt.text = arg1.raiseVal.toString();
                this._raiseValue = arg1.raiseVal;
                if (arg1.isHadBook) 
                {
                    this.selectTime(arg1.hour, false);
                    this.enableRadio(false);
                    if (ClockManager.instance.nowDate.getHours() >= arg1.hour) 
                    {
                        this._timeTxt.htmlText = Language.getString(57478);
                    }
                }
                else 
                {
                    this.enableRadio(true);
                }
            }
            return;
        }

        public updateContribute(arg1: int): void
        {
            return;
        }

        public updateGold(arg1: int): void
        {
            this._goldTxt.text = arg1.toString();
            return;
        }

        public updateRaise(arg1: int): void
        {
            this._raiseValue = arg1;
            this._raiseTxt.text = arg1.toString();
            return;
        }

        public updateByDiscountInfo(arg1: GuildPartyDiscountInfo): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc5=0;
            this._discountInfo = arg1;
            if (arg1) 
            {
                loc5 = 0;
                while (loc5 < this._typeTxtArray.length) 
                {
                    loc1 = this._partyConfigArray[loc5] as TGuildPartyConfig;
                    loc3 = loc1.costContribution * arg1.contriDiscount / 100;
                    loc4 = loc1.costGold * arg1.goldDiscount / 100;
                    loc2 = this._typeTxtArray[loc5] as Object /* flash.text.TextField */;
                    loc2.htmlText = Language.getStringByParam(57422 + loc5, loc4);
                    ++loc5;
                }
            }
            return;
        }

        /* internal  */enableRadio(arg1: Boolean): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=0;
            while (loc4 < this._radioArray.length) 
            {
                loc1 = this._radioArray[loc4] as GRadioButton;
                loc1.enabled = arg1;
                loc2 = this._selectSpriteArray[loc4] as Object /* flash.display.Sprite */;
                loc2.mouseEnabled = arg1;
                loc3 = loc1.value as int;
                if (!arg1 && loc3 == this._partyInfo.type.value()) 
                {
                    EffectManager.glowFilterReg(loc1, [FilterConst.uiBtnFilter]);
                }
                else 
                {
                    EffectManager.glowFilterUnReg(loc1);
                }
                ++loc4;
            }
            return;
        }

        /* internal  */enableTimeBtnByCurrentTime(): void
        {
            var loc1=null;
            var loc4=0;
            var loc2=ClockManager.instance.nowDate;
            var loc3=loc2.getHours();
            var loc5=0;
            while (loc5 < this._timeBtnArray.length) 
            {
                loc1 = this._timeBtnArray[loc5] as GButton;
                if ((loc4 = int(loc1.name)) <= loc3) 
                {
                    loc1.enabled = false;
                }
                else 
                {
                    loc1.enabled = true;
                }
                ++loc5;
            }
            return;
        }

        /* internal  */onDescBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildPartyDescWindowOpen));
            return;
        }

        /* internal  */onTimeBtnClickHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc1=arg1.target as GButton;
            if (loc1.styleName != "GuildPartyTimeSelectBtn") 
            {
                this.selectTime(int(loc1.name));
            }
            return;
        }

        /* internal  */selectTime(arg1: int, arg2: Boolean=true): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._timeBtnArray.length) 
            {
                loc1 = this._timeBtnArray[loc2] as GButton;
                if (loc1.enabled) 
                {
                    if (loc1.name != arg1.toString()) 
                    {
                        loc1.styleName = "GuildPartyTimeBtn";
                        loc1.enabled = arg2;
                    }
                    else 
                    {
                        loc1.styleName = "GuildPartyTimeSelectBtn";
                    }
                }
                ++loc2;
            }
            this._selectTime = arg1;
            this._timeTxt.htmlText = Language.getStringByParam(57417, arg1.toString());
            return;
        }

        /* internal  */onCreateBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var partyType: EGuildPartyType;
            var partyConfig: TGuildPartyConfig;
            var realCostContri: int;
            var realCostGold: int;
            var costGold: int;
            var onBtnClickHandler: Function;

            var loc1;
            partyType = null;
            realCostContri = 0;
            realCostGold = 0;
            costGold = 0;
            e = arg1;
            if (this._partyInfo && this._partyInfo.isHadBook) 
            {
                MsgManager.showRollTipsMsg(Language.getString(57433));
                return;
            }
            if (this._selectTime == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(57434));
                return;
            }
            partyType = EGuildPartyType.convert(this._typeRadioGroup.selectedData as int);
            partyConfig = GuildPartyConfig.instance.getPartyConfig(partyType.__value);
            if (partyConfig) 
            {
                onBtnClickHandler = (arg1: int): void
                {
                    if (arg1 == Alert.OK) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.GuildBookParty, {"type": partyType, "hour": _selectTime}));
                        Alert.resetToDefault();
                    }
                    return;
                }
                realCostContri = partyConfig.costContribution;
                realCostGold = partyConfig.costGold;
                if (this._discountInfo) 
                {
                    realCostContri = partyConfig.costContribution * this._discountInfo.contriDiscount / 100;
                    realCostGold = partyConfig.costGold * this._discountInfo.goldDiscount / 100;
                }
                if (Cache.instance.role.money.gold + this._raiseValue / 10 < realCostGold) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(57436));
                    return;
                }
                if (this._raiseValue / 10 >= realCostGold) 
                {
                    costGold = 0;
                }
                else 
                {
                    costGold = realCostGold - this._raiseValue / 10;
                }
                Alert.show(Language.getStringByParam(57486, this._selectTime, GuildUtil.getGuildPartyTypeName(partyType.__value), costGold), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
            }
            return;
        }

        /* internal  */onSelectClickHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc1=int((arg1.target as Object /* flash.display.Sprite */).name);
            (this._radioArray[loc1] as GRadioButton).selected = true;
            return;
        }

        /* internal  */onJoinClickHandler(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildPartyRaiseContributeOpen));
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            this._goldTxt.text = Cache.instance.role.money.gold.toString();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetPartyInfo));
            this.enableTimeBtnByCurrentTime();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildPartyGetDiscount));
            return;
        }

        /* internal */ /* var */_typeRadioGroup: RadioButtonGroup;

        /* internal */ /* var */_timeTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_descBtn: GButton;

        /* internal */ /* var */_createBtn: GButton;

        /* internal */ /* var */_raiseTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_joinTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_descTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_goldTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_partyRewardTxt: GTextFiled;

        /* internal */ /* var */_timeBtnArray: Array<any>;

        /* internal */ /* var */_selectTime: int;

        /* internal */ /* var */_partyInfo: GuildPartyInfo;

        /* internal */ /* var */_partyConfigArray: Array<any>;

        /* internal */ /* var */_radioArray: Array<any>;

        /* internal */ /* var */_selectSpriteArray: Array<any>;

        /* internal */ /* var */_raiseValue: int;

        /* internal */ /* var */_discountInfo: GuildPartyDiscountInfo;

        /* internal */ /* var */_typeTxtArray: Array<any>;
    }
